package logic

import (
	"cart_srv/model/mysql"
	"context"
	"gorm.io/gorm"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCartCheckedGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCartCheckedGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCartCheckedGoodsLogic {
	return &GetCartCheckedGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCartCheckedGoodsLogic) GetCartCheckedGoods(in *cart.GetCartCheckedGoodRequest) (*cart.GetCartCheckedGoodsResponse, error) {
	// todo: add your logic here and delete this line
	c := mysql.Cart{
		Model: gorm.Model{ID: uint(in.UserId)},
	}
	list, err := c.CartList()
	if err != nil {
		return nil, err
	}
	var totalNum int64
	var totalPrice int64
	for _, v := range list {
		totalNum += v.GoodsNum
		totalPrice += v.GoodsPrice * v.GoodsNum
	}
	return &cart.GetCartCheckedGoodsResponse{
		GoodNum:   totalNum,
		GoodPrice: totalPrice,
	}, nil
}
